const app = getApp(); //引入导航

var util = require('../../utils/common.js'); //引入公共JS 


import { FareAdd } from 'FareAdd.model.js';
var fareAdd = new FareAdd();
Page({
  data: {
    showIcon: true,
    //显示返回键以及首页
    idSelect: true,
    classify: "二代身份证",
    selectID: "0",
    adultFlag: true,
    studentFlag: true,
    childFlag: true,
    tName: "",
    tId: "",
    tPhone: "",
    showMessage: false
  },
  onLoad: function (options) {},
  idOpen: function (e) {
    this.setData({
      idSelect: false
    });
  },
  idClose: function () {
    this.setData({
      idSelect: true
    });
  },
  idSelect: function (e) {
    this.setData({
      classify: e.currentTarget.dataset.text
    });
  },
  selectCk: function (e) {
    var _num = e.currentTarget.dataset.num; //下标 

    this.setData({
      selectID: _num
    });
  },
  nameInput: function (e) {
    //监听实名登记姓名输入
    this.setData({
      tName: e.detail.value
    });
  },
  idInput: function (e) {
    //监听实名登记姓名输入
    this.setData({
      tId: e.detail.value
    });
  },
  phoneInput: function (e) {
    //监听实名登记姓名输入
    this.setData({
      tPhone: e.detail.value
    });
  },
  formSubmit: function (e) {
    console.log(this.data.tName);
    console.log(this.data.tId);
    console.log(this.data.tPhone);
    console.log(this.data.classify);

    if (this.data.selectID == 0) {
      var selectID = '成人';
    } else if (this.data.selectID == 1) {
      var selectID = '儿童';
    } else if (this.data.selectID == 2) {
      var selectID = '学生';
    }

    var telRule = /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[1|3|5|8|9])[0-9]{8}$/,
        hzRule = /^[a-zA-Z0-9]{5,17}$/,
        hxzRule = /(H|M)(\d{10})$/,
        tbz = /^([0-9]{8}|[0-9]{10})$/,
        IdRule = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;

    if (this.data.tName == '') {
      this.showMessage('姓名不能为空');
    } else if (this.data.tPhone == '') {
      this.showMessage('手机号不能为空');
    } else if (!telRule.test(this.data.tPhone)) {
      this.showMessage('请输入正确的手机号');
    } else if (this.data.tId == '') {
      this.showMessage('证件号不能为空');
    } else if (this.data.tId != '') {
      if (this.data.classify == '二代身份证' && !IdRule.test(this.data.tId)) {
        this.showMessage('请输入正确的身份证号');
      } else if (this.data.classify == '护照' && !hzRule.test(this.data.tId)) {
        this.showMessage('请输入正确的护照');
      } else if (this.data.classify == '回乡证' && !hxzRule.test(this.data.tId)) {
        this.showMessage('请输入正确的回乡证');
      } else if (this.data.classify == '台胞证' && !tbz.test(this.data.tId)) {
        this.showMessage('请输入正确的台胞证');
      } else {
        fareAdd.PushIdCard(this.data.tName, this.data.tId, this.data.tPhone, this.data.classify, selectID, res => {
          console.log(res);

          if (res == 1) {
            this.showMessage('新增成功！');
            var pages = getCurrentPages(); //当前页面

            var beforePage = pages[pages.length - 2]; //前一页

            tt.navigateBack({
              success: function () {
                beforePage.onLoad(); // 执行前一个页面的onLoad方法
              }
            });
          } else {
            this.showMessage('新增失败，请稍后再试！');
          }
        });
      }
    }
  },
  showMessage: function (text) {
    var that = this;
    that.setData({
      showMessage: true,
      messageContent: text
    });
    setTimeout(function () {
      that.setData({
        showMessage: false,
        messageContent: ''
      });
    }, 3000);
  }
});